package com.hxkcloud.nurse.popup;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;

import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.utils.BrightnessUtils;
import com.hxkcloud.nurse.wheelview.OnWheelScrollListener;
import com.hxkcloud.nurse.wheelview.WheelView;
import com.hxkcloud.nurse.wheelview.adapter.NumericWheelAdapter;

import java.util.Calendar;

/**
 * Created by ray_lei on 2016/7/14.
 */
public class DateTimeSelectPopWindow extends PopupWindow implements View.OnClickListener, OnWheelScrollListener {
    private Activity context;
    private WheelView wheel_year;
    private WheelView wheel_month;
    private WheelView wheel_day;
    private WheelView wheel_hour;
    private WheelView wheel_minute;

    private int curYear;
    private int curMonth;
    private int curDate;
    private int curHour;
    private int curMin;

    private Button bt_cancel;
    private Button bt_sure;

    public DateTimeSelectPopWindow(Activity context) {
        this.context = context;
        View rootView = LayoutInflater.from(context).inflate(R.layout.popup_selectymdhm, null);
        setContentView(rootView);

        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);

        setFocusable(true);
        setTouchable(true);
        setOutsideTouchable(true);
        setBackgroundDrawable(new ColorDrawable(0x00000000));

        setAnimationStyle(R.style.popupwindow_anim_default);
        initViews(rootView);
        initDatas();
        initListener();
    }

    private void initDatas() {
        Calendar c = Calendar.getInstance();
        curYear = c.get(Calendar.YEAR);
        curMonth = c.get(Calendar.MONTH) + 1;//通过Calendar算出的月数要+1
        curDate = c.get(Calendar.DATE);
        curHour = c.get(Calendar.HOUR_OF_DAY);
        curMin = c.get(Calendar.MINUTE);

        NumericWheelAdapter numericWheelAdapter1 = new NumericWheelAdapter(context, 1970, curYear);
        numericWheelAdapter1.setLabel(context.getString(R.string.pop_timeSelector_year));
        wheel_year.setViewAdapter(numericWheelAdapter1);
        wheel_year.setCyclic(true);//是否可循环滑动

        NumericWheelAdapter numericWheelAdapter2 = new NumericWheelAdapter(context, 1, 12, "%02d");
        numericWheelAdapter2.setLabel(context.getString(R.string.pop_timeSelector_month));
        wheel_month.setViewAdapter(numericWheelAdapter2);
        wheel_month.setCyclic(true);

        initDay(curYear, curMonth);
        wheel_day.setCyclic(true);


        NumericWheelAdapter numericWheelAdapter3 = new NumericWheelAdapter(context,0, 23, "%02d");
        numericWheelAdapter3.setLabel(context.getString(R.string.pop_timeSelector_hour));
        wheel_hour.setViewAdapter(numericWheelAdapter3);
        wheel_hour.setCyclic(true);

        NumericWheelAdapter numericWheelAdapter4 = new NumericWheelAdapter(context,0, 59, "%02d");
        numericWheelAdapter4.setLabel(context.getString(R.string.pop_timeSelector_minute));
        wheel_minute.setViewAdapter(numericWheelAdapter4);
        wheel_minute.setCyclic(true);



        //设置显示行数
        wheel_year.setVisibleItems(7);
        wheel_month.setVisibleItems(7);
        wheel_day.setVisibleItems(7);
        wheel_hour.setVisibleItems(7);
        wheel_minute.setVisibleItems(7);


    }

    private void initDay(int curYear, int curMonth) {
        NumericWheelAdapter numericWheelAdapter = new NumericWheelAdapter(context, 1, getDay(curYear, curMonth), "%02d");
        numericWheelAdapter.setLabel(context.getString(R.string.pop_timeSelector_day));
        wheel_day.setViewAdapter(numericWheelAdapter);
    }

    private void initListener() {
        bt_sure.setOnClickListener(this);
        bt_cancel.setOnClickListener(this);
        wheel_year.addScrollingListener(this);
        wheel_month.addScrollingListener(this);
        wheel_day.addScrollingListener(this);
        wheel_hour.addScrollingListener(this);
        wheel_minute.addScrollingListener(this);
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                BrightnessUtils.lightOn(context);
            }
        });
    }

    private void initViews(View rootView) {
        wheel_year = (WheelView) rootView.findViewById(R.id.wheel_selectymdhm_year);
        wheel_month = (WheelView) rootView.findViewById(R.id.wheel_selectymdhm_month);
        wheel_day = (WheelView) rootView.findViewById(R.id.wheel_selectymdhm_day);
        wheel_hour = (WheelView) rootView.findViewById(R.id.wheel_selectymdhm_hour);
        wheel_minute = (WheelView) rootView.findViewById(R.id.wheel_selectymdhm_minute);
        bt_cancel = (Button) rootView.findViewById(R.id.bt_selectymdhm_cancel);
        bt_sure = (Button) rootView.findViewById(R.id.bt_selectymdhm_sure);
    }

    /**
     * @param year
     * @param month
     * @return
     */
    private int getDay(int year, int month) {
        int day = 30;
        boolean flag = false;
        switch (year % 4) {
            case 0:
                flag = true;
                break;
            default:
                flag = false;
                break;
        }
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                day = 31;
                break;
            case 2:
                day = flag ? 29 : 28;
                break;
            default:
                day = 30;
                break;
        }
        return day;
    }

    public void show(View parent) {
        setCurTime();
        showAtLocation(parent, Gravity.BOTTOM, 0, 0);
        BrightnessUtils.lightOff(context);
    }

    public void setCurTime() {
        Calendar c = Calendar.getInstance();
        curYear = c.get(Calendar.YEAR);
        curMonth = c.get(Calendar.MONTH) + 1;//通过Calendar算出的月数要+1
        curDate = c.get(Calendar.DATE);
        curHour = c.get(Calendar.HOUR_OF_DAY);
        curMin = c.get(Calendar.MINUTE);

        wheel_year.setCurrentItem(curYear - 1970);
        wheel_month.setCurrentItem(curMonth - 1);
        wheel_day.setCurrentItem(curDate - 1);
        wheel_hour.setCurrentItem(curHour);
        wheel_minute.setCurrentItem(curMin);
    }

    @Override
    public void onClick(View v) {
        dismiss();
        switch (v.getId()) {
            case R.id.bt_selectymdhm_sure:
                if (timeListener != null) {
                    int month = (wheel_month.getCurrentItem() + 1);
                    String displayMonth = "";
                    int day = (wheel_day.getCurrentItem() + 1);
                    String displayDay = "";
                    int hour = (wheel_hour.getCurrentItem());
                    String displayHour = "";
                    int minute = (wheel_minute.getCurrentItem());
                    String displayMinute = "";
                    if(month<10){
                        displayMonth = "0"+month;
                    }else{
                        displayMonth = ""+month;
                    }
                    if(day<10){
                        displayDay = "0"+day;
                    }else{
                        displayDay = ""+day;
                    }
                    if(hour<10){
                        displayHour = "0"+hour;
                    }else{
                        displayHour = ""+hour;
                    }
                    if(minute<10){
                        displayMinute = "0"+minute;
                    }else{
                        displayMinute = ""+minute;
                    }
                    timeListener.setTime((wheel_year.getCurrentItem() + 1970) + "."
                            + (displayMonth) + "."
                            + (displayDay) + " " + (displayHour) + ":" + (displayMinute));
                }
                break;
            case R.id.bt_selectymdhm_cancel:

                break;
        }
    }

    @Override
    public void onScrollingStarted(WheelView wheel) {

    }

    @Override
    public void onScrollingFinished(WheelView wheel) {
        int n_year = wheel_year.getCurrentItem() + 1970;//年
        int n_month = wheel_month.getCurrentItem() + 1;//月

        initDay(n_year, n_month);
    }

    public interface TimeListener {
        void setTime(String time);
    }

    private TimeListener timeListener;

    public void setTimeListener(TimeListener timeListener) {
        this.timeListener = timeListener;
    }
}
